package org.jinion.configuration;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jinion.constants.ConfiguraionPropertyEnum;
import org.jinion.constants.DatabaseTypeEnum;
import org.jinion.constants.StringConstant;
import org.jinion.log.message.configuration.DBconfigMessage;

public class DBconfig {
	private static final Log log = LogFactory.getLog(DBconfig.class);
	
	private String driveClass;
	private String username;
	private String password;
	private DatabaseTypeEnum dbType;
	private String connectionUrl;
	
	public String getConnectionUrl() {
		return connectionUrl;
	}

	public void setConnectionUrl(String connectionUrl) {
		this.connectionUrl = connectionUrl;
	}

	public DatabaseTypeEnum getDbType() {
		return dbType;
	}

	public void setDbType(DatabaseTypeEnum dbType) {
		this.dbType = dbType;
	}

	public String getDriveClass() {
		return driveClass;
	}

	public void setDriveClass(String driveClass) {
		this.driveClass = driveClass;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public static DBconfig createDefaultDBconfig() {
		return createDBconfig(StringConstant.DEFAULT_CONFIG_FILE.getValue());
	}
	
	public static DBconfig createDBconfig(String propertyName) {
		Properties properties = getProperties(propertyName);
		if(null != properties) {
			if(checkRequiredProperties(properties)) {
				DBconfig config = new DBconfig();
				config.setDriveClass(properties.getProperty(ConfiguraionPropertyEnum.driverClass.getPropertyName()));
				config.setUsername(properties.getProperty(ConfiguraionPropertyEnum.username.getPropertyName()));
				config.setPassword(properties.getProperty(ConfiguraionPropertyEnum.password.getPropertyName()));
				config.setDbType(parseDBtype(properties.getProperty(ConfiguraionPropertyEnum.databaseName.getPropertyName())));
				config.setConnectionUrl(properties.getProperty(ConfiguraionPropertyEnum.connectionUrl.getPropertyName()));
				return config;
			} else {
				log.error(DBconfigMessage.M1.getMessageContent());
				return null;
			}
		} else {
			log.error(DBconfigMessage.M2.getMessageContent());
			return null;
		}
	}
	
	private static Properties getProperties(String propertyFile){
		Properties properties = new Properties();
		InputStream stream = ClassLoader.getSystemResourceAsStream(propertyFile);
		if (null == stream) {
			stream = Thread.currentThread().getContextClassLoader().getResourceAsStream(propertyFile);
		}
		try {
			properties.load(stream);
			return properties;
		} catch (IOException e) {
			log.error(DBconfigMessage.M3.getMessageContent());
			return null;
		} 
	}
	
	private static boolean checkRequiredProperties(Properties properties) {
		String driveClass = properties.getProperty(ConfiguraionPropertyEnum.driverClass.getPropertyName());
		String username = properties.getProperty(ConfiguraionPropertyEnum.username.getPropertyName());
		String password = properties.getProperty(ConfiguraionPropertyEnum.password.getPropertyName());
		String dbType = properties.getProperty(ConfiguraionPropertyEnum.databaseName.getPropertyName());
		String connectionUrl = properties.getProperty(ConfiguraionPropertyEnum.connectionUrl.getPropertyName());
		if(null == driveClass || driveClass.length() == 0) {
			log.error(DBconfigMessage.M4.getMessageContent());
			return false;
		}
		if(null == username || username.length() == 0) {
			log.error(DBconfigMessage.M5.getMessageContent());
			return false;
		}
		if(null == password || password.length() == 0) {
			log.error(DBconfigMessage.M6.getMessageContent());
			return false;
		}
		if(null == dbType || dbType.length() == 0) {
			log.error(DBconfigMessage.M7.getMessageContent());
			return false;
		}
		if(null == connectionUrl || connectionUrl.length() == 0) {
			log.error(DBconfigMessage.M8.getMessageContent());
			return false;
		}
		return true;
	}
	
	private static DatabaseTypeEnum parseDBtype(String dbname) {
		if(DatabaseTypeEnum.mysql.getDbname().equalsIgnoreCase(dbname)) {
			return DatabaseTypeEnum.mysql;
		} else if(DatabaseTypeEnum.oracle.getDbname().equalsIgnoreCase(dbname)) {
			return DatabaseTypeEnum.oracle;
		} else if(DatabaseTypeEnum.sqlServer.getDbname().equalsIgnoreCase(dbname)) {
			return DatabaseTypeEnum.sqlServer;
		} else {
			return DatabaseTypeEnum.unsupportDB;
		}
	}
}
